export const recursion = (arr) => {
  arr.forEach((item) => {
    delete item.cId
    delete item.pId
    item.ruterdata = item.ruterData
    delete item.ruterName
    delete item.ruterData
    if (item.children.length === 0) {
      delete item.children
    } else {
      recursion(item.children)
    }
  })
}

export const tree = (data) => {
  let map = {}
  for (let item of data) {
    map[item.id] = item
  }
  let result = [] //存放数组
  for (let item of data) {
    item.children = [] //给每个数组添加一个空children
    if (item.pId === 0) {
      result.push(item)//最上级的标签
    } else {
      //相当于用这个 parentId 当做父Id去查找对比查找数据
      let parent = map[item.pId]
      //添加到刚刚定义children数组中去
      parent.children.push(item)
    }
  }
  recursion(result)

  return result
}